package com.cskaoyan.catmall.controller.wx;

import com.cskaoyan.catmall.model.bo.WxGoodsListBo;
import com.cskaoyan.catmall.model.pojo.MarketUser;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.WxGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 创建日期: 2022-01-10-19-35
 * author : LL
 * description: 前台商品页面
 */
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {

    @Autowired
    WxGoodsService wxGoodsService;

    @GetMapping("count")
    public BaseRespVo getGoodsCount() {
        int goodsCount = wxGoodsService.getGoodsCount();
        return BaseRespVo.ok(goodsCount);
    }

    @GetMapping("category")
    public BaseRespVo getCategoryById(Integer id) {
        WxCategoryVo categoryVo = wxGoodsService.getCategoryById(id);
        return BaseRespVo.ok(categoryVo);
    }

    @GetMapping("list")
    public BaseRespVo getGoodsByCategoryId(WxGoodsListBo goodsListBo) {
        PageHelper.startPage(goodsListBo.getPage(), goodsListBo.getLimit());
        WxGoodsVo goodsVo = wxGoodsService.getGoodsSelective(goodsListBo);
        List<WxGoodsVo.ListDTO> list = goodsVo.getList();
        PageInfo<WxGoodsVo.ListDTO> pageInfo = new PageInfo<>(list);
        goodsVo.setTotal((int) pageInfo.getTotal());
        goodsVo.setPages(pageInfo.getPages());
        goodsVo.setLimit(goodsListBo.getLimit());
        goodsVo.setPage(goodsListBo.getPage());


        return BaseRespVo.ok(goodsVo);
    }

    @GetMapping("detail")
    public BaseRespVo getGoodsDetail(Integer id) {
        WxGoodsDetailVo goodsDetailVo = wxGoodsService.getGoodsDetailByGoodsId(id);
        MarketUser principal = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (principal != null) {
            wxGoodsService.insertFootPrint(principal,id);
        }
        if (goodsDetailVo == null) {
            return BaseRespVo.fail("商品已下架~");
        }
        return BaseRespVo.ok(goodsDetailVo);
    }

    @GetMapping("related")
    public BaseRespVo getRelatedGoods(Integer id) {

        WxRelatedGoodsVo relatedGoods = wxGoodsService.getRelatedGoods(id);

        return BaseRespVo.ok(relatedGoods);
    }


}